﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using School.Entities;
using School.Common;
using System.Data.Entity;
using School.Admission;
using System.Text;



namespace School.Pages.Admission
{
    public partial class File_Order : System.Web.UI.Page
    {
        //***************** This is function to handle page when page load ***************************
        //-**********************************************************************




        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1006) == true)
                {

                    if (!(Page.IsPostBack))
                    {

                        Fillcrls();
                        InitializeControls();
                        GridDataBind();
                    }

                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        # region "Collect Data"

        //***************** This is function to bind data grid  ***************************
        //-**************************************************************************
        protected void GridDataBind()
        {
            Int32 mycount;
            Hashtable SearchTable = new Hashtable();
            if (txtSearchPaymentNo.Text.Length > 0)
            {
                SearchTable.Add("Payment_Order_Number", txtSearchPaymentNo.Text.ToString());
            }
            if (txtSearchFirstName.Text.Length > 0)
            {
                SearchTable.Add("FirstName", txtSearchFirstName.Text.ToString());
            }
            if (txtSearchDate.Text.Length > 0)
            {
                SearchTable.Add("Date", txtSearchDate.Text.ToString());
            }
            IEnumerable<ADM_File_Order> Results = FileOrder.All(SearchTable);
            griddata.DataSource = Results;

            griddata.DataBind();
            griddata.Visible = true;
            mycount = Results.Count();
            lblRecordNo.Text = "عدد نتائج البحث = " + mycount.ToString();
            if (mycount == 0)
            {
                lblMessage.Text = "لا يوجد بيانات";
            }
            else
            {
                lblMessage.Text = "";
            }

        }

        # endregion


        # region "Fill all crls"

        //***************** This is function to fill crl  ***************************
        //-**************************************************************************

        protected void Fillcrls()
        {

            ddlcampus_Id.DataSource = Structure.GetCampuses();
            ddlcampus_Id.DataTextField = "Name";
            ddlcampus_Id.DataValueField = "Id";
            ddlcampus_Id.DataBind();
            ddlcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            ddlSearchcampus_Id.DataSource = Structure.GetCampuses();
            ddlSearchcampus_Id.DataTextField = "Name";
            ddlSearchcampus_Id.DataValueField = "Id";
            ddlSearchcampus_Id.DataBind();
            ddlSearchcampus_Id.Items.Insert(0, new ListItem("اختيار", "-1"));

            
            
            long expenseId = 0;
            try
            {

                expenseId = FileOrder.Expen_ID("100");
                txtPrice.Text = FileOrder.Expense_Value(expenseId).ToString();
            }
            catch
            {
                txtPrice.Text = "0";
            }
            txtDate.Text = DateTime.Today.ToShortDateString() ;
            txtSearchDate.Text  = DateTime.Today.ToShortDateString();
        }


        //***************** This is function to initlaize crl  ***************************
        //-**************************************************************************
        protected void InitializeControls()
        {
            txtFirstName.Text = "";
            txtSecondName.Text = "";
            txtThirdName.Text = "";
            txtFourthName.Text = "";
            lblMessage.Text = "";
            lblMessageAdd.Text = "";
        }

        # endregion


        # region "Mode Handlers"

        //***************** This is function to handle page in add mode  ***************************
        //-**************************************************************************
        protected void PutFormInAddMode()
        {
            txtFirstName.Text = "";
            txtSecondName.Text = "";
            txtThirdName.Text = "";
            txtFourthName.Text = "";
        }


        //***************** This is function to handle page in brows mode  ***************************
        //-**************************************************************************

        protected void PutFormInBrwosMode()
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;

        }

        #endregion


        # region "Grid Events ***********"

        //***************** This is function to handle page when Page Index Changing   ***************************
        //-**************************************************************************

        protected void griddata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griddata.PageIndex = e.NewPageIndex;
            GridDataBind();
        }


        //***************** This is function to handle page when Row Deleting  ***************************
        //-**************************************************************************

        protected void griddata_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                griddata.SelectedIndex = e.RowIndex;

                long CID = Int32.Parse(griddata.SelectedDataKey.Value.ToString());
                Boolean successadd = FileOrder.Delete(CID);
                if (successadd == true)
                {
                    GridDataBind();
                    lblMessage.Visible = true;
                    lblMessage.Text = "تم حذف البيانات بنجاح";

                }
            }
            catch (Exception)
            {
                lblMessage.Text = "  لم يتم حذف البيانات بنجاح رجاء مراجعة مدير النظام";

            }

        }


        //***************** This is function to handle page when Row Data Bound  ***************************
        //-**************************************************************************
        protected void griddata_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton LB = (LinkButton)e.Row.FindControl("LinkButton1");
                LB.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to delete record(s) ( " +
                DataBinder.Eval(e.Row.DataItem, "Payment_Order_Number") + " ) ?')");

            }
        }

        protected void griddata_SelectedIndexChanged(object sender, EventArgs e)
        {
            pnlAdd.Visible = false;
            pnlbrows.Visible = false;
            pnldetails.Visible = true;
            DetailsView1bind();
        }

        protected void DetailsView1bind()
        {
            Session["FileOrderId"] = griddata.SelectedDataKey.Value.ToString();
           

            Response.Redirect("Reports/Showreport.aspx");
        }



        # endregion



        # region   "Execute commands sent to the form"



        //***************** This is function to handle page when Puser click in الغاء btn  ***************************
        //-**************************************************************************
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = "";
            txtSecondName.Text = "";
            txtThirdName.Text = "";
            txtFourthName.Text = "";
        }


        //***************** This is function to handle page when Puser click in اضافة btn  ***************************
        //-**************************************************************************

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            InitializeControls();
            pnlAdd.Visible = true;
            pnlbrows.Visible = false;
            pnldetails.Visible = false;
        }

        //***************** This is function to handle page when Puser click in انهاء btn  ***************************
        //-**************************************************************************


        protected void btnend_Click(object sender, EventArgs e)
        {
            btnAdd.Visible = true;
            pnlAdd.Visible = false;
            pnlbrows.Visible = true;
        }
        //***************** This is function to handle page when Puser click in حفظ btn  ***************************
        //-**************************************************************************

        protected void btnSave_Click(object sender, EventArgs e)
        {
            
            Int64 campus = Convert.ToInt64(ddlcampus_Id.SelectedValue);
            Int64 certifcate = Convert.ToInt64(ddlCertificate_Id.SelectedValue );
            Int64 Stage = Convert.ToInt64(ddlStage_Id.SelectedValue );
            Int64 grade = Convert.ToInt64(ddlGrade_Id.SelectedValue );
            DateTime dt=Convert.ToDateTime(txtDate.Text);
            long paymentNo = Structure.GetPayment_Order_Number();
            Int64 xx = FileOrder.Add(new ADM_File_Order() { Payment_Order_Number=paymentNo, Price = Convert.ToDecimal(txtPrice.Text), Certificate_Id = certifcate, Stage_Id = Stage, Grade_Id = grade, Date = dt, FirstName = txtFirstName.Text, SecondName = txtSecondName.Text, ThirdName = txtThirdName.Text, FourthName = txtFourthName.Text,Campus_Id=campus });

            Session["FileOrderId"] = xx;
            InitializeControls();

            string strbScript = "";
            strbScript = "<script language='javascript'>";
            strbScript = strbScript + "window.open('Reports/Showreport.aspx', true, 'height=600,width=800,status=no,toolbar=no,menubar=no,location=no')";
            strbScript = strbScript + "</script>";

            Response.Write(strbScript);
            lblMessageAdd.Text = "تم حفظ البيانات بنجاح";

        }


        # endregion



        # region " Search *********************"




        //***************** This is function to search by  Get All btn ***************************
        //-**************************************************************************
        protected void btnGetAll_Click(object sender, EventArgs e)
        {
            
            
            GridDataBind();
        }


        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GridDataBind();
        }

        # endregion

        protected void ddlCertificate_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate_Id.SelectedIndex > 0)
            {
                ddlStage_Id.Items.Clear();
                ddlGrade_Id.Items.Clear();
                ddlStage_Id.DataSource = Structure.GetStagesByCertificate(Convert.ToInt64(ddlCertificate_Id.SelectedValue));
                ddlStage_Id.DataTextField = "Name";
                ddlStage_Id.DataValueField = "id";
                ddlStage_Id.DataBind();
                ddlStage_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlStage_Id.Enabled = true;
                ddlGrade_Id.Enabled = false;
            }
        }

        protected void ddlStage_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStage_Id.SelectedIndex > 0)
            {
                ddlGrade_Id.Items.Clear();
                ddlGrade_Id.DataSource = Structure.GetGradesByStage(Convert.ToInt64(ddlStage_Id.SelectedValue));
                ddlGrade_Id.DataTextField = "Name";
                ddlGrade_Id.DataValueField = "id";
                ddlGrade_Id.DataBind();
                ddlGrade_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlGrade_Id.Enabled = true;
            }
        }

        protected void ddlGrade_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        protected void ddlcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlcampus_Id.SelectedIndex > 0)
            {
                ddlCertificate_Id.Items.Clear();
                ddlGrade_Id.Items.Clear();
                ddlStage_Id.Items.Clear();
                ddlCertificate_Id.DataSource = Structure.GetCertificatesByCampus(Convert.ToInt64(ddlcampus_Id.SelectedValue));
                ddlCertificate_Id.DataTextField = "Name";
                ddlCertificate_Id.DataValueField = "Id";
                ddlCertificate_Id.DataBind();
                ddlCertificate_Id.Items.Insert(0, new ListItem("اختيار", "-1"));
                ddlCertificate_Id.Enabled = true;
                ddlGrade_Id.Enabled = false;
                ddlStage_Id.Enabled = false;
            }
        }

        protected void txtShowCampus_TextChanged(object sender, EventArgs e)
        {

        }

        protected void ddlSearchcampus_Id_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            
        }

        protected void ddlGrade_Id_SelectedIndexChanged1(object sender, EventArgs e)
        {

        }

        protected void btnrptPrint_Click(object sender, EventArgs e)
        {
            Session["FileOrderId"] = "1004";
            
            Response.Redirect("Reports/Showreport.aspx");
            
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Session["FileOrderId"] = "1006";
        }


    }
}